//
//  LXJVisitorViewController.swift
//  XJWeiBo
//
//  Created by 李旭杰 on 2017/3/27.
//  Copyright © 2017年 李旭杰. All rights reserved.
//

import UIKit

/// 访客视图控制器
class LXJVisitorViewController: UITableViewController {
    
    /// 是否登录的标记
    var isLogin = LXJOAuthViewModel.sharedOAuthViewModel.isLogin
    
    /// 访客视图
    var visitorView: LXJVisitorView?
    
    
    /// 设置登录视图
    override func loadView() {
        /// 非登录状态下设置登录视图
        if !isLogin {
            /// 创建访客视图
            visitorView = LXJVisitorView()
            /// 赋值访客视图的代理对象
            //  visitorView?.delegate = self
            /// 赋值访客视图的闭包属性 注意循环引用
            visitorView?.registerAndLoginClosure = {[weak self] in
                self?.loadOAuthController()
            }
            /// 设施导航条
            setupNavItemUI()
            
            self.view = visitorView
        } else{/// 登录状态下设置正常的视图
            super.loadView()
        }
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
    }
    
    /// 设置导航条按钮
    @objc private func setupNavItemUI() -> Void {
        /// 注册摁扭
        self.navigationItem.leftBarButtonItem = UIBarButtonItem(title: "注册", target: self, action: #selector(registerAction))
        /// 登录摁扭
        self.navigationItem.rightBarButtonItem = UIBarButtonItem(title: "登录", target:self, action: #selector(loginAction))
    }
    
    // MARK: 登录注册按钮的功能实现
    func registerAction() -> Void {
        loadOAuthController()
    }
    
    func loginAction() -> Void {
        loadOAuthController()
    }
    
    // MARK: 点击登录和注册摁扭是 Modal 出授权登录界面
    func loadOAuthController() -> Void {
        let oAuthVC = LXJOAuthController()
        /// navvc 嵌套 vc
        let oAuthNavVC = LXJNavigationController(rootViewController: oAuthVC)
        
        present(oAuthNavVC, animated: true, completion: nil)
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}

// MARK: 实现 visitor 的代理方法
extension LXJVisitorViewController: LXJVisitorViewDelegate{
    func loadLoginController() {
        loadOAuthController()
    }
}
